import Vue from 'vue'
import Router from 'vue-router'

import login from "@/components/login";
import HomePage from "@/components/HomePage";
import PostDetail from "@/components/PostDetail";
import UserZone from "@/components/UserZone";
import register from "@/components/register";
import ForumDetail from "@/components/ForumDetail";
import Detail from "@/components/Detail";
import PostForum from "@/components/PostForum";
import SerachBar from "@/components/SerachBar";
import MyInfo from "@/components/MyInfo";

Vue.use(Router)
var router = new Router({
    routes: [
        {path: '/', redirect: '/HomePage/n'},
        {path: '/login', name: 'Login', component: login},
        {path: '/register', name: 'Register', component: register},
        {path: '/HomePage/:tag', name: 'HomePage', component: HomePage},
        {path: '/ForumDetail/:pid', name: 'ForumDetail', component: ForumDetail},
        {path: '/postdetail/:pid', name: 'PostDetail', component: PostDetail},
        {path: '/Detail/:pid', name: 'Detail', component: Detail},
        {path: '/PostForum', name: 'PostForum', component: PostForum},
        {path: '/SerachBar', name: 'SerachBar', component: SerachBar},
        {path: '/userzone/:uid', name: 'UserZone', component: UserZone},
        {path: '/myinfo/:uid', name: 'MyInfo', component: MyInfo}
    ]
})
/**
 * url拦截:
 * 访问其他页面之前先检查token
 */
router.beforeEach((to,from,next)=>{
	if(to.path==="/login"||to.path==="/register"){
		next();
	}
	else{
		const tokenStr=window.sessionStorage.getItem("token");
		if(!tokenStr){
			next("/login");
			location.reload();
		}
		else{
			next();
		}
	}

})
export default router
